<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="户主姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'householdName', validatorRules.householdName]" placeholder="请输入户主姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'idNumber', validatorRules.idNumber]" placeholder="请输入身份证号"></a-input>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="乡镇和自然村" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-cascader
                :options="options"
                style="width: 100%"
                @change="onChange"
                :value="cascaderValue"
                placeholder="请输入乡镇和自然村"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="家庭地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'address', validatorRules.address]" placeholder="请输入家庭地址"></a-input>
            </a-form-item>
          </a-col>

        </a-row>


        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择出生日期" v-decorator="[ 'birthDate', validatorRules.birthDate]"
                      :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <!--        0_男，1_女-->
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['sex', validatorRules.sex]" :trigger-change="true" dictCode="sex"
                                 placeholder="请选择性别"/>
            </a-form-item>
          </a-col>

        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="家庭人口" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'famillyNumber', validatorRules.famillyNumber]" placeholder="请输入家庭人口"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="文化程度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'education', validatorRules.education]" placeholder="请输入文化程度"></a-input>
            </a-form-item>
          </a-col>

        </a-row>

        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入联系方式"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <a-form-item label="信用等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['creditRating',validatorRules.creditRating]" :trigger-change="true"
                                 dictCode="credit_rating" placeholder="请选择信用等级"/>
            </a-form-item>
          </a-col>

        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <a-form-item label="信用评分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-tooltip>
                <template slot="title">
                  范围：1~100
                </template>
                <a-input v-decorator="[ 'creditScore', validatorRules.creditScore]" placeholder="请输入信用评分"></a-input>
              </a-tooltip>
            </a-form-item>
          </a-col>
          <a-col :lg="12">
            <!--        0_贫困户，1_非贫困户-->
            <a-form-item label="是否贫困" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isPoor',, validatorRules.isPoor]" :trigger-change="true" dictCode="is_poor"
                                 placeholder="请选择是否贫困"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="1">
          <a-col :lg="12">
            <!--        0_脱贫仍享受政策、1_未脱贫、2_稳定脱贫-->
            <a-form-item label="贫困户状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['poorHouseholdStatus',, validatorRules.poorHouseholdStatus]" :trigger-change="true"
                                 dictCode="poor_household_status" placeholder="请选择贫困户状态"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
    import {httpAction, getAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "CreditRatingModal",
        components: {
            JDate,
            JDictSelectTag,
        },
        data() {
            return {
                cascaderValue: [],
                defaultValue: [],
                options: [],
                form: this.$form.createForm(this),
                selectValue: "",
                title: "操作",
                width: 1000,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {
                    householdName: {rules: [{ required: true, message: '户主名称不能为空'}]},
                    creditRating: {rules: [{ required: true, message: '信用等级不能为空'}]},
                    creditScore: {rules: [{ required: true, message: '信用评分不能为空'}]},
                    fwzId: {rules: [{ required: true, message: '乡镇不能为空'}]},
                    fwbId: {},
                    isPoor: {rules: [{ required: true, message: '请选择是否贫困'}]},
                    idNumber: {rules: [{ required: true, message: '身份证号不能为空'}]},
                    birthDate: {rules: [{ required: true, message: '出生日期不能空'}]},
                    sex: {rules: [{ required: true, message: '性别不能为空'}]},
                    famillyNumber: {},
                    education: {},
                    phone: {rules: [{ required: true, message: '联系方式不能为空'}]},
                    address: {rules: [{ required: true, message: '家庭地址不能为空'}]},
                    poorHouseholdStatus: {},
                },
                url: {
                    add: "/creditsystem/creditRating/add",
                    edit: "/creditsystem/creditRating/edit",
                }
            }
        },
        created() {
        },
        methods: {
            onChange(value, selectedOptions) {
                if (value.length > 0) {
                    this.cascaderValue = value
                    this.fwbId = selectedOptions[1].code;
                    this.fwzId = selectedOptions[0].code;
                }
            },
            filter(inputValue, path) {
                return (path.some(option => (option.label).toLowerCase().indexOf(inputValue.toLowerCase()) > -1));
            },
            add() {
                this.edit({});
                this.cascaderValue = []
            },
            edit(record) {
                const that = this
                this.fwzId = record.fwzId;
                this.fwbId = record.fwbId;
                getAction("/financialservices/serviceDepartment/serviceDepartMentCascadedDropDown", "").then((res) => {
                    if (res.success) {
                        this.options = res.result
                    }
                    this.loading = false;
                })
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model,'address', 'householdName', 'creditRating', 'creditScore', 'fwzId', 'fwbId', 'isPoor', 'idNumber', 'birthDate', 'sex', 'famillyNumber', 'education', 'phone', 'poorHouseholdStatus'))
                })
                if (JSON.stringify(record) != "{}") {
                    var params = {};
                    params.fwzId = record.fwzId;
                    params.fwbId = record.fwbId;
                    getAction("/financialservices/serviceDepartment/cascadedDropDownEchoResults", params).then((res) => {
                        if (res.success) {
                            that.cascaderValue = res.result
                        }
                        this.loading = false;
                    })
                }

            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        let httpurl = '';
                        let method = '';
                        // alert("id: " + this.model.id)
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        values.fwbId = this.fwbId;
                        values.fwzId = this.fwzId;
                        if (!values.fwbId) {
                            that.$message.warning("所属自然村不能为空");
                            return ;
                        }
                        if (!values.fwzId) {
                            that.$message.warning("所属乡镇不能为空");
                            return ;
                        }
                        that.confirmLoading = true;
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row,'address', 'householdName', 'creditRating', 'creditScore', 'fwzId', 'fwbId', 'isPoor', 'idNumber', 'birthDate', 'sex', 'famillyNumber', 'education', 'phone', 'poorHouseholdStatus'))
            }

        }
    }
</script>